// pages/cart/cart.js
const util =require('../../utils/util')
const api =require('../../config/api')
Page({
  data: {
    cartList:[],//购物车数据
    cartTotal:[],//购物车其他信息
    goodsCount:'',//商品数量
    checked:false,//购物车商品是否选中
    // productIds:'',//商品id 没用
    checkedAll:false,//是否全选
    // 商品总价
    priceAll:0,
    cartListId:[],//拿到购物车内所有的id
    editProductId:[],//在编辑页面拿到选中后的id
    editShow:true,//进入编辑模式
    edit:'编辑',//编辑按钮
    buyOrDelete:'下单',//下单或删除按钮
  },
  // 步进器
  onChange(event) {
    console.log(event.detail);
  },
  // 进入编辑或完成编辑
  goEdit(){
    let that=this
      if(that.data.edit=='编辑'){
      this.setData({
        editShow:true,
      })
      this.setData({
        edit:that.data.editShow?'完成':'编辑',
        buyOrDelete:that.data.editShow?'删除':'下单',
        editProductId:[],
      })
    }else if(that.data.edit=='完成'){
      this.setData({
        editShow:false,
      })
      this.setData({
        edit:that.data.editShow?'完成':'编辑',
        buyOrDelete:that.data.editShow?'删除':'下单',
        editProductId:[],
      })
    }
    // 选中不同商品 拿到 删除商品所需要的productId 
    this.data.cartList.filter(item=>item.checked).forEach(item=>{
      if(that.data.editProductId.length<that.data.cartList.length){
        that.setData({
          editProductId:that.data.editProductId.concat(item.productId)
        })
      }
    })
   
  },
  // 购买或者删除
  buyOrDelete(){
    let that =this
    console.log(that.data.buyOrDelete);
    if(that.data.buyOrDelete=='删除'){
      console.log(1);
      if(that.data.editProductId.length>0){
        wx.showModal({
          title: '提示',
          content: '是否删除',
          success (res) {
          if (res.confirm) {
            util.request(api.cartDeleted,{
              productIds:that.data.editProductId
            },'post').then(res=>{
              console.log(res);
              if(res.errno==0){
                that.setData({
                  cartList:res.data.cartList,
                  cartTotal:res.data.cartTotal,
                  priceAll:res.data.cartTotal.checkedGoodsAmount,
                })
              }
            })
            wx.showToast({
              title: '删除成功',
              duration: 2000,
              icon:'none'
              });
          } else if (res.cancel) {
          console.log('用户点击取消')
          }
          }
          })
      }
    }
  },
  // 点击切换选择
  chanceRadio(e){
    let that =this
    this.data.cartList.forEach(item=>{
      if(that.data.cartListId.length>=that.data.cartList.length){
      }else{
        that.setData({
          cartListId:that.data.cartListId.concat(item.productId)
        })
      }
    })
    util.request(api.cartChecked,{
      productIds:[e.currentTarget.dataset.productid],
      isChecked:e.currentTarget.dataset.checked?0:1
    },'post').then(res=>{
      console.log(res);
      if(res.errno==0){
        that.setData({
          cartList:res.data.cartList,
          cartTotal:res.data.cartTotal,
          priceAll:res.data.cartTotal.checkedGoodsAmount,
          editProductId:[]
        })
        // 通过判断选中数和商品总数 来判断是否全选
        that.setData({
          checkedAll:res.data.cartTotal.checkedGoodsCount==res.data.cartTotal.goodsCount?true:false
        })
        // 选中不同商品 拿到 删除商品所需要的productId 
        this.data.cartList.filter(item=>item.checked).forEach(item=>{
          if(that.data.editProductId.length<that.data.cartList.length){
            that.setData({
              editProductId:that.data.editProductId.concat(item.productId)
            })
          }
        })
      }
      
    })
    
  },
  // 实现全选和反选
  checkedAll(e){
    // console.log(e);
    let that =this 
    this.data.cartList.forEach(item=>{
      if(that.data.cartListId.length>=that.data.cartList.length){
      }else{
        that.setData({
          cartListId:that.data.cartListId.concat(item.productId)
        })
      }
    })
    this.setData({
      checkedAll:that.data.checkedAll?false:true
    })

    util.request(api.cartChecked,{
      productIds:that.data.cartListId,
      isChecked:that.data.checkedAll?1:0
    },'post').then(res=>{
      // console.log(res);
      if(res.errno==0){
        that.setData({
          cartList:res.data.cartList,
          cartTotal:res.data.cartTotal,
          priceAll:res.data.cartTotal.checkedGoodsAmount,
          editProductId:[],
        })
        // 选中不同商品 拿到 删除商品所需要的productId 
        this.data.cartList.filter(item=>item.checked).forEach(item=>{
          if(that.data.editProductId.length<that.data.cartList.length){
            that.setData({
              editProductId:that.data.editProductId.concat(item.productId)
            })
          }
        })
      }
      
    })
    
  },
  // ------------------onload 加载
  onShow:function(){
    let that =this
    this.getCartGoods()
  },
  getCartGoods:function(){
    let that =this
    util.request(api.cartIndex).then(res=>{
      console.log(res);
      if(res.errno==0){
        that.setData({
          cartList:res.data.cartList,
          goodsCount:res.data.cartTotal.goodsCount,

        })
        console.log(that.data.cartList);
      }
    })
    util.request(api.cartChecked,{
      productIds:that.data.cartListId,
      isChecked:that.data.checkedAll?0:1
    },'post').then(res=>{
      console.log(res);
      if(res.errno==0){
        that.setData({
          cartList:res.data.cartList,
          cartTotal:res.data.cartTotal,
          priceAll:res.data.cartTotal.checkedGoodsAmount
        })
      }
    })
  } 
})